// ==================== src/event.rs ====================
//! 事件系统定义

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
use std::fmt::Debug;
use crate::Result;

/// 事件类型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EventType {
    /// 配置变更
    ConfigChange,
    /// 服务注册
    ServiceRegister,
    /// 服务注销
    ServiceDeregister,
    /// 实例健康状态变更
    InstanceHealthChange,
    /// 集群节点变更
    ClusterNodeChange,
    /// 心跳事件
    Heartbeat,
}

/// 事件数据
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Event {
    /// 事件ID
    pub event_id: String,
    /// 事件类型
    pub event_type: EventType,
    /// 事件时间
    pub timestamp: DateTime<Utc>,
    /// 事件数据
    pub data: serde_json::Value,
    /// 命名空间
    pub namespace: String,
    /// 分组
    pub group: Option<String>,
    /// 服务名
    pub service_name: Option<String>,
}

/// 事件监听器trait
#[async_trait]
pub trait EventListener: Send + Sync + Debug {
    /// 处理事件
    async fn handle_event(&self, event: &Event) -> Result<()>;
    
    /// 获取监听器名称
    fn name(&self) -> &str;
    
    /// 获取感兴趣的事件类型
    fn interested_events(&self) -> Vec<EventType>;
}

/// 事件发布器trait
#[async_trait]
pub trait EventPublisher: Send + Sync {
    /// 发布事件
    async fn publish(&self, event: Event) -> Result<()>;
    
    /// 订阅事件
    async fn subscribe(&self, listener: Box<dyn EventListener>) -> Result<()>;
    
    /// 取消订阅
    async fn unsubscribe(&self, listener_name: &str) -> Result<()>;
}